﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using DTO;
using Model;

namespace DataService
{
    public class AdminService
    {
        public const string ADMIN_SESSION = "A!dmin@Web";

        public Admin Login(string username, string password)
        {
            using (BaseModel model = new BaseModel())
            {
                object obj = model.GetValueFromDB(0,"GetInfoLoginAccountAdmin", username, password);

                if (obj != null)
                {
                    if (Convert.ToBoolean(obj))
                    {
                        return new Admin()
                        {
                            Username = username,
                            Password = password
                        };
                    }
                }
            }

            return null;
        }
        public IList<Customer> GetCustomers()
        {
            using (AccountModel model = new AccountModel())
            {
                DataTable dt= model.GetListAccounts();
                if (dt.Rows.Count > 0)
                {
                    IList<Customer> customers = new List<Customer>();
                    foreach (DataRow row in dt.Rows)
                    {
                        Customer customer = new Customer(BaseModel.GetIntFieldValue(row,AccountModel.ACCOUNT_ID_FIELD));
                        customer.Code = BaseModel.GetStringFieldValue(row, AccountModel.ACCOUNT_NAME_FIELD);
                        customer.CompanyName = BaseModel.GetStringFieldValue(row, "CompanyName");
                        customer.MainContact = new Contact(BaseModel.GetIntFieldValue(row, "AddressId"));
                        customer.MainContact.FirstName = BaseModel.GetStringFieldValue(row, AccountModel.FIRST_NAME_FIELD);
                        customer.MainContact.LastName = BaseModel.GetStringFieldValue(row, AccountModel.LAST_NAME_FIELD);
                        customer.Created = BaseModel.GetDateTimeFieldValue(row, AccountModel.DATE_CREATE_FIELD);


                        customers.Add(customer);
                    }

                    return customers;
                }
                return null;
            }
        }

        public IList<DTO.User> GetListUsersByAccountId(int AccountId)
        {
            using (AccountModel model = new AccountModel())
            {
                DataTable dt = model.GetListUserByAccountId(AccountId);
                if (dt.Rows.Count > 0)
                {
                    IList<User> users = new List<User>();
                    foreach (DataRow row in dt.Rows)
                    {
                        User user = new User(BaseModel.GetIntFieldValue(row, "UserId"));
                        user.Username = BaseModel.GetStringFieldValue(row, "UserName");
                        user.IsActived = BaseModel.GetBooleanFieldValue(row, "IsActive");
                        user.IsAdmin = BaseModel.GetBooleanFieldValue(row, "UserIsAdmin");
                        user.FirstName = BaseModel.GetStringFieldValue(row, "FirstName");
                        user.LastName = BaseModel.GetStringFieldValue(row, "LastName");
                        user.Email = BaseModel.GetStringFieldValue(row, "Email");
                        user.Phone = BaseModel.GetStringFieldValue(row, "PhoneNotes");
                        users.Add(user);
                    }
                    return users;
                }
                return null;
            }
        }

        public void DeleteAccountByAccountId(int AccountId)
        {
            using (AccountModel model = new AccountModel())
            {
                model.DeleteAccount(AccountId);
            }
        }
    }
}
